package com.wicket.okrbff.biz.service.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.wicket.okrbff.biz.service.dto.req.AddGetUserIrInfoFromRedisReqDto;
import com.wicket.okrbff.biz.service.dto.req.ClearTokenFromRedisReqDto;
import com.wicket.okrbff.biz.service.dto.req.ClearUserInfoFromRedisReqDto;
import com.wicket.okrbff.biz.service.dto.req.GetUserBasicInfoFromRedisReqDto;
import com.wicket.okrbff.biz.service.dto.res.AddGetUserIrInfoFromRedisRespDto;
import com.wicket.okrbff.biz.service.dto.res.ClearTokenFromRedisRespDto;
import com.wicket.okrbff.biz.service.dto.res.ClearUserInfoFromRedisRespDto;
import com.wicket.okrbff.biz.service.dto.res.GetUserBasicInfoFromRedisRespDto;
import com.wicket.okrbff.common.util.RedisUtil;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.common.redis.BaseUserInfoForRealRedis;
import com.wicket.okrframework.common.redis.UserIndInfoForRedis;
import com.wicket.okrframework.integration.dto.OrgInductionRecordDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
@BanAuto
public class NbMember {
    /**
     * code:clearUserInfoFromRedis
     * name:Redis清空用户信息
     * desc:undefined
     **/
    @Resource
    private RedisUtil redisClient;

    @BanAuto
    public ClearUserInfoFromRedisRespDto clearUserInfoFromRedis(ClearUserInfoFromRedisReqDto reqDto) {
        if (reqDto.getOriginalRoleMemberId() == null || reqDto.getOriginalRoleMemberId().isEmpty()) {
            return new ClearUserInfoFromRedisRespDto();
        }
        redisClient.delete("userbase-" + reqDto.getOriginalRoleMemberId());
        redisClient.delete("inductionInfo-" + reqDto.getOriginalRoleMemberId());
        return new ClearUserInfoFromRedisRespDto();
    }

    /**
     * code:clearTokenFromRedis
     * name:Redis清空用户Token
     * desc:undefined
     **/
    @BanAuto
    public ClearTokenFromRedisRespDto clearTokenFromRedis(ClearTokenFromRedisReqDto reqDto) {
        String token = (String) redisClient.get("token-" + reqDto.getOriginalRoleMemberId());
        if (token == null || token.isEmpty()) {
            return new ClearTokenFromRedisRespDto();
        }
        redisClient.delete(token);
        redisClient.delete("token-" + reqDto.getOriginalRoleMemberId());
        return new ClearTokenFromRedisRespDto();
    }

    /**
     * code:getUserBasicInfoFromRedis
     * name:Redis获取用户基本信息
     * desc:undefined
     **/
    @BanAuto
    public GetUserBasicInfoFromRedisRespDto getUserBasicInfoFromRedis(GetUserBasicInfoFromRedisReqDto reqDto) {
        GetUserBasicInfoFromRedisRespDto retData = new GetUserBasicInfoFromRedisRespDto();
        String str = (String) redisClient.get("userbase-" + reqDto.getOriginalRoleMemberId());
        if (str == null || str.isEmpty()) {
            return null;
        }
        BaseUserInfoForRealRedis redisData = JSONObject.parseObject(str, BaseUserInfoForRealRedis.class);
        BeanUtil.copyProperties(redisData, retData, "birthday");
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if (redisData.getBirthday() != null && !redisData.getBirthday().isEmpty()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String times = sdf.format(Long.valueOf(redisData.getBirthday()));
                date = ft.parse(times);
                retData.setBirthday(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return retData;
    }

    /**
     * code:getUserIrInfoFromRedis
     * name:Redis获取用户就职信息
     * desc:undefined
     **/
    @BanAuto
    public AddGetUserIrInfoFromRedisRespDto addGetUserIrInfoFromRedis(AddGetUserIrInfoFromRedisReqDto reqDto) {
        AddGetUserIrInfoFromRedisRespDto retData = new AddGetUserIrInfoFromRedisRespDto();
        String str = (String) redisClient.get("inductionInfo-" + reqDto.getOriginalRoleMemberId());
        if (str == null || str.isEmpty()) {
            return retData;
        }
        UserIndInfoForRedis redisRetData = JSONObject.parseObject(str, UserIndInfoForRedis.class);
        if (redisRetData.getUserIndList() == null) {
            retData.setOrgInductionRecordList(new ArrayList<>());
        } else {
            retData.setOrgInductionRecordList(redisRetData.getUserIndList().stream().map(org -> {
                OrgInductionRecordDto retElm = new OrgInductionRecordDto();
                BeanUtil.copyProperties(org, retElm);
                return retElm;
            }).collect(Collectors.toList()));
        }
        return retData;
    }
    //手工接入方法
}
